<script setup lang='ts'>
  import { nextTick, reactive, ref } from 'vue';
  import { systemApi } from '@/api/controller';
  import { ToastSuccess, Toastloading } from '@/utils/public';
  const formLabelWidth = '120px'
  const dataForm = ref()
  const $emit = defineEmits(['updateList'])
  const action = reactive({
    title:'',
    dialogFormVisible:false,
    disabled:false,
    promptlyTypeList:[] as Array<any>
  })
  interface formQuert {
    id:number
    promptly_type:number
    word_desc:string
  }
  let form = reactive({} as formQuert)
  const formRules = {
    word_desc: [{ required: true, message: "请输入黑名单语言", trigger: "blur" }],
    promptly_type: [{ required: true, message: "请选择封禁状态", trigger: "blur" }],
  }
  const handleSubmit = ()=>{
    dataForm.value.validate(async(valid:any)=>{
      if(!valid) return
      let submitSet = false
      const { id } = form
      const loading = Toastloading()
      if(!id) {
        await systemApi.addBlackWord(form).then(()=>{
          submitSet = true
          ToastSuccess('新增成功')
        }).catch(()=>{
          loading.close()
        })
      }else {
        await systemApi.editBlackWord(form).then(()=>{
          submitSet = true
          ToastSuccess('编辑成功')
        }).catch(()=>{
          loading.close()
        })
      }
      if(submitSet) {
        loading.close()
        closeDialog()
        $emit('updateList')
      }
    })
  }
  const openDialog = async(query:any = {})=> {
    const { 
      id, word_desc, promptlyTypeList=[], promptly_type=''
    } = query
    let formobj = {
      word_desc,
      promptly_type
    } as any
    if(id)formobj['id'] = id
    action.promptlyTypeList = promptlyTypeList
    Object.assign(form,formobj)
    action.title = id?'编辑黑名单语言':'新增黑名单语言'
    action.dialogFormVisible = true
  }
  const closeDialog = ()=>{
    form = reactive({} as formQuert)
    nextTick(() => {
      dataForm.value.clearValidate();
    });
    action.dialogFormVisible = false
  }
  defineExpose({
    openDialog
  })
</script>

<template>
  <div>
    <el-dialog
      top="10vh"
      class="xm-dialog input-width"
      v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled"
      :show-close="action.disabled"
      :close-on-press-escape="action.disabled"
      width="40%"
      :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="黑名单语言" prop="word_desc">
          <el-input
            v-model="form.word_desc"
            :autosize="{ minRows: 6, maxRows: 6}"
            type="textarea"
            placeholder="请输入黑名单语言"
          />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="是否立即封禁" prop="promptly_type">
          <el-select v-model="form.promptly_type" placeholder="请选择封禁状态">
            <el-option
            v-for="item in action.promptlyTypeList"
            :label="item.label"
            :value="item.type"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>